﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Controls;
using System.Xml.Serialization;

namespace BDSim
{
    //TypeConverter(typeof(PropertySorter))
    public abstract class BDSimObject: INotifyPropertyChanged
    {
        public BDSimObject()
        {
            
            if (MainDispatcher.MainWindowDispatcher == null)
            {
                Menu = new ContextMenu();
            }
            else
            {
                MainDispatcher.MainWindowDispatcher.Invoke(new Action(() =>
                {
                    Menu = new ContextMenu();
                }));
            }
        }

        #region 全局参量
        /// <summary>
        /// 全局开始时间
        /// </summary>
        [XmlIgnore]
        public static UTCTime GlobalStartTime = new UTCTime();
        /// <summary>
        /// 全局当前时间
        /// </summary>
        [XmlIgnore]
        public static UTCTime GlobalCurrentTime = new UTCTime();
        /// <summary>
        /// 全局结束时间
        /// </summary>
        [XmlIgnore]
        public static UTCTime GlobalEndTime = new UTCTime();
        /// <summary>
        /// 全局状态
        /// </summary>
        [XmlIgnore]
        public static ESimStatus GlobalStatus = ESimStatus.UnInited;
        /// <summary>
        /// 全局仿真步长，单位：s
        /// </summary>
        [XmlIgnore]
        public static double GlobalStepSize = 60;
        /// <summary>
        /// 当前仿真步数
        /// </summary>
        [XmlIgnore]
        public static int GlobalStepIndex = 0;

        #endregion

        #region 委托方法
        /// <summary>
        /// 显示目标属性的委托函数
        /// </summary>
        [XmlIgnore]
        public static Action<BDSimObject> DisplayObjectProperty;
        /// <summary>
        /// 输出仿真消息
        /// </summary>
        [XmlIgnore]
        public static Action<SimInfo> DlgReportSimInfo;
        /// <summary>
        /// 输出属性设置消息
        /// </summary>
        [XmlIgnore]
        public static Action<string> DlgReportPropertyInfo;
        /// <summary>
        /// 任务执行报告
        /// </summary>
        [XmlIgnore]
        protected static Action DlgAddCompleteMission;
        #endregion

        #region 基本属性
        protected string _Name;
        private Guid _Uid = Guid.NewGuid();
        private int _Id;
        private bool _IsSelected = false;
        private ContextMenu _Menu;

        [Browsable(false), XmlAttribute]
        public Guid Uid
        {
            get { return _Uid; }
            set { _Uid = value; NotifyPropertyChanged("Uid"); }
        }

        [Browsable(false), XmlAttribute]
        public int Id
        {
            get { return _Id; }
            set { _Id = value; NotifyPropertyChanged("Id"); }
        }

        [Browsable(false),XmlIgnore]
        public ContextMenu Menu
        {
            get
            {
                return _Menu;  
            }
            set {_Menu = value;}
        }

        [Browsable(false),XmlIgnore]
        public bool IsSelected
        {
            get { return _IsSelected; }
            set 
            {
                _IsSelected = value;
                if (_IsSelected)
                {
                    DisplayObjectProperty(this);
                }
            }
        }
        #endregion

        #region 抽象方法
        public virtual void Init() 
        {
            //执行任务报告
            //if (GlobalStatus == ESimStatus.Initing && DlgAddCompleteMission!=null)
            //{
            //    DlgAddCompleteMission();
            //}
        }
        public virtual void StepRun() { }
        #endregion

        #region //属性通知
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

    }
}
